<?php
/**
 * 生成beauty 目录下的东西 
 *
 * @filesource .php
 * @package 
 * @subpackage 
 * @version $id: 0.1, utf8, $
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2011 LD pr.
 * @link 
 * @example 
 *   
 */

require_once dirname(dirname(__FILE__)) .  '/_library/opentaobao.php';
require_once dirname(dirname(__FILE__)) .  '/_library/zcredit.php';

//配置文件目录
define('CONFIG_DIR', dirname(__FILE__).'/_configs/');
define('SHORT_DIR', dirname(__FILE__).'/vip/');
ini_set('register_argc_argv', 'On');

$a = new beauty($argv);

class beauty{

    public $site_home = 'http://girl-city.com/beauty/';

    public $main = array(
        //减肥药排行榜
        'index' => 'data_index.php',
        //10大丰胸
        'fx' => 'data_fx.php',
        //10大美白
        'mb' => 'data_mb.php',
        //10大左选肉
        'qd' => 'data_qd.php',
        //10大瘦腿
        'st' => 'data_st.php',
    );//END $mian

    //统计代码和其他JS
    public $script_file = 'data_script.php';

    // 开始
    public function __construct($argv){
        if( empty($argv[1])){
            exit('no params!');
        } 

        if('main'==$argv[1]){
            $this->make_main();
        }
        else{
            exit('no page!');
        }

    }//END func __construct



    public function make_main(){
        $data = array();
        foreach($this->main as $k=>$v){
            $data[$k] = include( CONFIG_DIR . $v );
        }

        $tabs= array();
        foreach($data as $k=>$v){
            $tabs[$k] = array(
                'checked' => false,
                'color'   => $v['tabcolor'],
                'uri'     => "{$k}.htm",
                'name'    => $v['tab'],
            );

        }
        foreach($data as $k=>$v){
            $data[$k]['tabs'] = $tabs;
            $data[$k]['tabs'][$k]['checked'] = true;

            $data[$k]['style'] = $v['addon_css'];
            $data[$k]['script'] = file_get_contents( CONFIG_DIR . $this->script_file );
            
            $num_iids = $v['top_items'];
            
            $api = new OpenTaobao();
            $api->set_time_out(15);
            $api->app_key = OpenTaobao::DEFAULT_APPKEY;
            $api->secret_code = OpenTaobao::DEFAULT_SECRET_CODE;
            $api->method = 'taobao.taobaoke.items.convert';
            $api->fields = 'iid,num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume,taobaoke_cat_click_url,keyword_click_url';
            $api->nick = 'kldoo';
            $api->num_iids = implode(',', $num_iids);
            $response = $api->get();
            
            $items = $response["taobaoke_items_convert_response"]["taobaoke_items"]["taobaoke_item"];

           //过期处理 
            if( count($items) != count($num_iids) ){
                $num_iids_b = $num_iids;
                foreach($items as $it){
                    $key_b = array_search($it['num_iid'], $num_iids_b);
                    unset($num_iids_b[$key_b]);
                }
                
                $error = "ERROR: page={$k}.htm, not found num_iid: ".implode(',', $num_iids_b)."\t\n";
                exit($error);
            }

            //ico
            foreach($items as $ik=>$it){
                $items[$ik]['seller_credit_ico'] = Zcredit::taobao_credit_ico($it['seller_credit_score']);
                $items[$ik]['click_url']  = $this->get_shorturl($it['click_url'], $it['num_iid']);
                $it['volume'] = empty($it['volume']) ? 15 : intval($it['volume']); 
                if(100 > $it['volume']){
                    $it['volume'] = ceil( 3.14*$it['volume']);
                }else{
                    $it['volume'] = ceil( (3.14*$it['volume']) * (2/3) );
                }
                $items[$ik]['volume'] = $it['volume'];
            }  
            
            //sort
            usort($items, 'usort_volume');
            $data[$k]['top_items'] = $items;
        }//END foreach

        //生成文件
        foreach($data as $k=>$v){
            $file_name = dirname(__FILE__) . "/$k.htm";        
            $file_content = '';

            extract($v, EXTR_OVERWRITE);
            ob_start();
            include( CONFIG_DIR . 'template_main.php' );
            $file_content = ob_get_clean();
            
            file_put_contents($file_name, $file_content);
        }

    }//END func make_main



    public function get_shorturl($url=null, $number=null){
        if( !empty($number) AND is_numeric($number) ){
            $short_code = base_convert($number, 10, 36);
        } 

        $short_url = $this->site_home . 'vip/' . $short_code . '.php';  
        $short_file = SHORT_DIR . $short_code . '.php'; 
        $short_content = "<?php header('Location:{$url}');";
        
        file_put_contents($short_file, $short_content);

        return $short_url; 
    }//END func get_shorturl 

}//END class


function usort_volume($a, $b){
    $a = intval($a['volume']);
    $b = intval($b['volume']); 

    if($a==$b) return 0;
    return ($a > $b) ? -1 : 1; 
}//END func usort_volume 


//END FILE

